var appInstance = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shop_name:'',
    order_status:'',
    order_list:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      shop_name:appInstance.globalData.shop_name
    });

    var t = this;
    if( options.status ){

      this.setData({
        order_status:options.status
      },function(){
        t.order_list();
      });

    }else{

      t.order_list();

    }

  },

  //todo 获取订单列表信息
  order_list:function( resolve ){

    var t = this;
    var url = appInstance.globalData.api_path + '&r=checkout.order.get_list';
    var data = {
      status: this.data.order_status,
    };

    appInstance.request(url, data, "GET", res => {

      if( res.data.list ){

        t.setData({
          order_list: res.data.list
        });
      }
      if( typeof resolve == 'function'){
        resolve( res.data );
      }
    });

  },

  changeOrderStatus:function( e ){

    var order_status = e.currentTarget.dataset.status;

    this.setData({
      order_status:order_status
    });

    //todo 获取 该状态下的订单列表
    this.order_list();



  },


  orderPay:function( e ){

    //todo 调启微信支付
    var url = appInstance.globalData.api_path+'&r=checkout.xpay.index_xcx';

    var order = e.currentTarget.dataset.order;
    var data = {
      order_id: order.id,
    };

    var t = this;
    appInstance.request( url, data, 'GET', res=>{

      if( res.data.nonceStr ){
        wx.requestPayment({
          timeStamp: res.data.timeStamp,
          nonceStr: res.data.nonceStr,
          package: res.data.package,
          signType: res.data.signType,
          paySign: res.data.paySign,
          success (res) {
            // t.order_list();

            t.pay_notify( order );
          },
          fail (res) {

          }
        });

      }


    });

  },

  pay_notify:function( order ){

    var t = this;
    var url = appInstance.globalData.api_path+'&r=checkout.xpay.pay_notify';
    var data = {
      'order_id' : order.id
    };
    appInstance.request( url, data, 'POST', function(){

      t.order_list();
      /*var index = t.order_list.list.indexOf( order );

      t.data.order_list[ index ].is_payed = 1;
      t.setData({
        order_list:t.data.order_list
      });*/

    });

  },


  orderCancel:function( e ){

    var t = this;
    var url = appInstance.globalData.api_path + '&r=checkout.order.cancel';
    var data = {
      order_id: e.currentTarget.dataset.order.id,
    };

    appInstance.request(url, data, "GET", res => {

      t.order_list();

    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

    var p = new Promise( this.order_list );
    p.then(function( result ){
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})